﻿namespace SP2010.Enhancements.SEO
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;

    internal class ViewStateFilter : Stream
    {
        private Stream _originalFilter;
        private string _topPageHiddenFields;

        public ViewStateFilter(Stream originalFilter)
        {
            try
            {
                this._originalFilter = originalFilter;
            }
            catch
            {
                throw;
            }
        }

        public override void Close()
        {
            try
            {
                this._originalFilter.Close();
            }
            catch
            {
                throw;
            }
        }

        private string ExtractPageTopHiddenFields(string html)
        {
            int index = html.IndexOf("<form");
            if (index >= 0)
            {
                int startIndex = html.IndexOf("<div", index);
                if (startIndex < 0)
                {
                    return html;
                }
                int num3 = html.IndexOf(">", startIndex);
                int num4 = num3 - startIndex;
                int num5 = html.IndexOf("</div", startIndex);
                int num6 = html.IndexOf(">", num5);
                int length = (num5 - num3) - 1;
                this._topPageHiddenFields = html.Substring(num3 + 1, length);
                html = html.Remove(startIndex, (num6 - startIndex) + 1);
            }
            return html;
        }

        public override void Flush()
        {
            try
            {
                this._originalFilter.Flush();
            }
            catch
            {
                throw;
            }
        }

        private string InsertExtractedHiddenFields(string html)
        {
            if (!string.IsNullOrEmpty(this._topPageHiddenFields))
            {
                int index = html.IndexOf("</form>");
                if (index > 0)
                {
                    html = html.Insert(index, this._topPageHiddenFields + Environment.NewLine);
                    this._topPageHiddenFields = null;
                }
            }
            return html;
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int num;
            try
            {
                num = this._originalFilter.Read(buffer, offset, count);
            }
            catch
            {
                throw;
            }
            return num;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            long num;
            try
            {
                num = this._originalFilter.Seek(offset, origin);
            }
            catch
            {
                throw;
            }
            return num;
        }

        public override void SetLength(long value)
        {
            try
            {
                this._originalFilter.SetLength(value);
            }
            catch
            {
                throw;
            }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] dst = new byte[count];
            Buffer.BlockCopy(buffer, offset, dst, 0, count);
            string html = Encoding.Default.GetString(buffer);
            html = this.ExtractPageTopHiddenFields(html);
            html = this.InsertExtractedHiddenFields(html);
            byte[] bytes = Encoding.Default.GetBytes(html);
            this._originalFilter.Write(bytes, 0, bytes.GetLength(0));
        }

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override long Length
        {
            get
            {
                return 0L;
            }
        }

        public override long Position { get; set; }
    }
}

